<?php

namespace Run;

class Company extends  Base {

	public function cacheCertificateFileNameByUser($params) {
		if (empty($params['start']) || !(int) $params['start']) {
			echo '请设置开始页数--start={start}' . PHP_EOL;exit;
		}
		if (empty($params['end']) || !(int) $params['end']) {
			echo '请设置开始页数--end={end}' . PHP_EOL;exit;
		}
		$start = (int) $params['start'];
		$end = (int) $params['end'];
		for ($companyId = $start; $companyId <= $end; $companyId++) {
			\Business\Company::instance()->getCertificateImages($companyId, false);
			echo $companyId . '/' . $end . PHP_EOL;
		}
		echo 'success' . PHP_EOL;
	}

	public function cacheCertificateFileName() {
		while ($companyId = \Lib\Db\Redis::instance()->lpop('init_cache_certificate_filename_company_ids')) {
			\Business\Company::instance()->getCertificateImages($companyId, false);
			echo $companyId . PHP_EOL;
		}
	}

	public function initCacheCertificateFileName() {
		$maxId = \Data\Company::instance()->getCompanyMaxId();
		$redis = \Lib\Db\Redis::instance();
		$redis->del('init_cache_certificate_filename_company_ids');
		for ($i = 1; $i <= $maxId; $i++) {
			$redis->rpush('init_cache_certificate_filename_company_ids', $i);
		}
	}

	public function processContacts() {
		$db = \Data\Base::instance();
		$allContacts = $db->from('Contacts')->findAll();

		$phoneContacts = array();
		foreach ($allContacts as $val) {
			$phoneContacts[$val['CellPhone']][] = $val;
		}
		foreach ($phoneContacts as $arr) {
			$nowId = 0;
			$replaceIds = array();
			foreach ($arr as $key => $val) {
				if (!$key) {
					$nowId = $val['Id'];
				} else {
					$replaceIds[] = $val['Id'];
				}
				$companyData = array(
					'ContactId' => $nowId,
					'CompanyId' => $val['CompanyId'],
					'BranchId' => $val['BranchId'],
					'Landline' => $val['Landline'],
					'Office' => $val['Office'],
					'CreateTime' => $val['CreateTime'],
					'Status' => $val['Status'],
				);
				$allIds =  array_merge($replaceIds, array($nowId));
				//TODO
				if (!$db->from('CompanyContacts')->where(array('ContactId' => $nowId,'CompanyId' => $val['CompanyId'],'BranchId' => $val['BranchId']))->findOne()) {
					$db->from('CompanyContacts')->insert($companyData);
				}
				//TODO 用户关联联系人
				$userContacts = $db->from('UserToContacts')->where(array('ContactId in' => $allIds))->findAll('ContactId');
				if ($userContacts) {
					if (!empty($userContacts[$nowId])) {
						if ($replaceIds) {
							$db->from('UserToContacts')->where(array('ContactId in' => $replaceIds))->del();
						}
					} else {
						$one = array_pop($userContacts);
						if ($userContacts) {
							$db->from('UserToContacts')->where(array('ContactId in' => array_keys($userContacts)))->del();
						}
						$db->from('UserToContacts')->where(array('Id' => $one['Id']))->update(array('ContactId' => $nowId));
					}
				}
    			//TODO 联系人收藏
				$db->from('FavoritesContacts')->where(array('ContactId in' => $allIds))->update(array('ContactId' => $nowId));
				//TODO 删除重复联系人
				if ($replaceIds) {
					$db->from('Contacts')->where(array('Id in' => $replaceIds))->del();
				}
			}
		}
	}

}